<?php
// 这是系统自动生成的公共文件

//字符串截取，过滤HTML标签

use think\exception\HttpResponseException;
use think\facade\Db;

function strip($str,$start=0,$length=0){
    return mb_substr(strip_tags(htmlspecialchars_decode($str)),$start,$length,"utf-8");
}



function rand_data($num=5,$cateid=""){

    if(empty($cate)){
        $ids = Db::table("xm_web_chapter")->field("id")->select()->toArray();
    }else{
        $ids = Db::table("xm_web_chapter")->where(['cateid'=>$cateid])->field("id")->select()->toArray();
    }
    
    if(!empty($ids)){
        $arr = array_map(function($item){
            return $item["id"];
        },$ids);
    }

    $keys = array_rand($arr,$num);

    $rand_ids=[];

    foreach ($keys as $key) {
        $rand_ids[$key]=$arr[$key];
    }

    $res = Db::table("xm_web_chapter")->where("id","in" ,$rand_ids)->select()->toArray();

    if(!empty($res)){
        for($i=0;$i<count($res);$i++){

            $cate = Db::table("xm_web_cate")->where(["id"=>$res[$i]["cateid"]])->find();

            $res[$i]["create_time"]=date("Y-m-d H:i:s",$res[$i]["create_time"]);
            $res[$i]["cover"]=$res[$i]["cover"] ? $res[$i]["cover"] : setdefaultimage($cate["alias"]);
        }
    }
    
    return $res;
}


function toRedirect(...$args){
    throw new HttpResponseException(redirect(...$args));
}
